package cn.com.dhc.zzz.action;

import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import cn.bbs.ba.base.BAAction;
import cn.bbs.ba.base.UserInfo;
import cn.bbs.ba.error.BAErrorMessage;
import cn.bbs.ba.factory.BABeanFactory;
import cn.bbs.ba.html.ZPaiConstant;
import cn.bbs.ba.util.CommonUntil;
import cn.com.zzz.dao.PPUserInfoDao;
import cn.com.zzz.html.PPJspContextServices;

public class AjaxCommonAction extends BAAction {
	
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		response.setHeader("Content-Type","text/html; charset=UTF-8");
		super.execute(mapping, form, request, response);
		
		PrintWriter writer = response.getWriter();
		
		String strType = CommonUntil.objToString(request.getParameter("type"));
		PPJspContextServices utb = new PPJspContextServices();
		// 取国际化资源
		if ("guojihua".equals(strType)) {
			writer.print(ZPaiConstant.getResouceDisplayAt(CommonUntil.objToString(request.getParameter("msgV"))));
		}
		// 每秒取
		else if ("time".equals(strType)) {
			
		}
		// 检查验证码输入的是否正确
		else if ("checkYanZhengMa".equals(strType)){
			String strCode = CommonUntil.objToString(request.getSession().getAttribute("captcha"));
			String strRndcode = CommonUntil.objToString(request.getParameter("rndcode"));
			// 验证码输入错误
			if (!strCode.equals(strRndcode)) {
				writer.print(BAErrorMessage.push("10006"));
			}
			else {
				writer.print("success");
			}
		}
		// 验证手机号码
		else if ("checkTel".equals(strType)){
			String strMobile = CommonUntil.objToString(request.getParameter("mobile"));
			
			String strcheckMailA = CommonUntil.objToString(request.getParameter("checkMobileA"));
			// 查看是否是本人的Mail
			if ("A".equals(strcheckMailA)) {
				if (!userInfo.getMobile().equals(strMobile)) {
					UserInfo userInfo = new UserInfo();
					userInfo.setMobile(strMobile);
					
					PPUserInfoDao userInfoDao = (PPUserInfoDao)BABeanFactory.getBean("userInfoDao");
					userInfo = userInfoDao.checkUserEx(userInfo);
					// 验证码输入错误
					if (userInfo != null) {
						writer.print(BAErrorMessage.push("10007"));
					}
					else {
						writer.print("success");
					}
				}
				else {
					writer.print("success");
				}
			}
			else {
				UserInfo userInfo = new UserInfo();
				userInfo.setMobile(strMobile);
				
				PPUserInfoDao userInfoDao = (PPUserInfoDao)BABeanFactory.getBean("userInfoDao");
				userInfo = userInfoDao.checkUserEx(userInfo);
				// 验证码输入错误
				if (userInfo != null) {
					writer.print(BAErrorMessage.push("10007"));
				}
				else {
					writer.print("success");
				}
			}

		}
		// 验证Email是否存在
		else if ("checkEmail".equals(strType)){
			String strMail = CommonUntil.objToString(request.getParameter("email"));
			
			String strcheckMailA = CommonUntil.objToString(request.getParameter("checkMailA"));
			// 查看是否是本人的Mail
			if ("A".equals(strcheckMailA)) {
				if (!userInfo.getEmail().equals(strMail)) {
					UserInfo userInfo = new UserInfo();
					userInfo.setEmail(strMail);
					
					PPUserInfoDao userInfoDao = (PPUserInfoDao)BABeanFactory.getBean("userInfoDao");
					userInfo = userInfoDao.checkUserEx(userInfo);
					if (userInfo != null) {
						writer.print(BAErrorMessage.push("10008"));
					}
					else {
						writer.print("success");
					}
				}
				else {
					writer.print("success");
				}
			}
			else {
				UserInfo userInfo = new UserInfo();
				userInfo.setEmail(strMail);
				
				PPUserInfoDao userInfoDao = (PPUserInfoDao)BABeanFactory.getBean("userInfoDao");
				userInfo = userInfoDao.checkUserEx(userInfo);
				if (userInfo != null) {
					writer.print(BAErrorMessage.push("10008"));
				}
				else {
					writer.print("success");
				}
			}

		}
		// 验证用户名是否存在
		else if ("checkUserName".equals(strType)){
			String strMobile = CommonUntil.objToString(request.getParameter("alias"));
			UserInfo userInfo = new UserInfo();
			userInfo.setAlias(strMobile);
			
			PPUserInfoDao userInfoDao = (PPUserInfoDao)BABeanFactory.getBean("userInfoDao");
			userInfo = userInfoDao.checkUserEx(userInfo);
			if (userInfo != null) {
				writer.print(BAErrorMessage.push("10009"));
			}
			else {
				writer.print("success");
			}
		}
		// 翻页 --正在竞拍，即将竞拍，晾单。。。。。
		else if ("pageTurn".equals(strType)) {
			
		}
		// 每秒刷新 --正在竞拍，即将竞拍，晾单。。。。。
		else if ("pageTurnBrich".equals(strType)) {
			
		}
		// 用户中心首页显示的内容
		else if ("UserCenterList".equals(strType)){

		}
		// 当前用户收货地址查询
		else if ("getAddressAll".equals(strType)){
			
		}
		// 检查当前pi地址是否已经注册过
		else if ("checkIpAdd".equals(strType)){
		}
		writer.flush();
		writer.close();
		return mapping.findForward("ajaxCommon");
	}

}
